package com.sinoglobal.sinostore.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.adapter.ShippingMethodAdapter;
import com.sinoglobal.sinostore.adapter.ViewHolder;
import com.sinoglobal.sinostore.bean.AddressListVo;
import com.sinoglobal.sinostore.bean.AddressVo;
import com.sinoglobal.sinostore.bean.ShipMethodVo;
import com.sinoglobal.sinostore.system.Constants;

import java.util.ArrayList;
/**
 * 
* @title: ShippingMethodActivity.java 
* @package com.chiliring.sinostore.activity 
* @description: TODO(配送方式) 
* @author LK
* @date 2015-1-16 上午9:49:07
 */
public class ShippingMethodActivity extends StateBaseActivity implements OnClickListener,OnItemClickListener {
	/**支持自取**/
	private static final String ISINVITE = "1";
	/**配送方式列表**/
//	@ViewInject(R.id.lv_method_list)
	private ListView lvShippingList;
	
	/**自取地址布局**/
//	@ViewInject(R.id.ll_ziqu_address)
	private LinearLayout llZiquAddress;
	
	/**查看全部自取地址**/
//	@ViewInject(R.id.btn_lookalladdress)
	private TextView btnLookAllAddress;
	
	private ShippingMethodAdapter methodAdapter;
	
	private String pId;
	private String shopId;
	private String goodsId;
	/**支持自取**/
	private String isSelf;
	/**配送方式**/
	ShipMethodVo entity;
	
	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_activity_shipping_method);
		openChenJInShi();
//		ViewUtils.inject(this);
		titleView.setText("配送方式");
		
		initView();
		
		initData();
	}
	
	private void initView(){
		lvShippingList=getViewById(R.id.lv_method_list);
		llZiquAddress=getViewById(R.id.ll_ziqu_address);
		btnLookAllAddress=getViewById(R.id.btn_lookalladdress);
		
		
		btnLookAllAddress.setOnClickListener(this);
		lvShippingList.setOnItemClickListener(this);
		templateButtonLeft.setOnClickListener(this);
	}
	
	private void initData(){
		Bundle bundle = getIntent().getExtras();
		pId = getIntent().getStringExtra("pId");
		shopId = getIntent().getStringExtra("shopId");
		goodsId = getIntent().getStringExtra("goodsId");
		isSelf = getIntent().getStringExtra("isSelf");
		ArrayList<ShipMethodVo> methods = (ArrayList<ShipMethodVo>) bundle.getSerializable("shipList");
		if(null == methods){
			this.finish();
			return;
		}
		methodAdapter = new ShippingMethodAdapter(this, methods);
		lvShippingList.setAdapter(methodAdapter);
		//支持自取
		if(ISINVITE.equals(isSelf)){
			getZiquAddress();
		}
		methodAdapter.setSelect(SubmitOrderFormActivity.oldposition);
		entity = (ShipMethodVo)methodAdapter.getItem(SubmitOrderFormActivity.oldposition);
		if(Constants.ISSELF_CODE.equals(entity.getCode())){
			llZiquAddress.setVisibility(View.VISIBLE);
		}else{
			llZiquAddress.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.btn_lookalladdress) {
			Bundle bundle = new Bundle();
			bundle.putString("pId", pId);
			bundle.putString("shopId", shopId);
			bundle.putString("goodsId", goodsId);
			toActivity(ZiquAddressActivity.class, bundle);
		} else if (id == R.id.shop_title_but_left) {
			choose();
		} else {
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
			choose();
			return true;
        }
		return super.onKeyDown(keyCode, event);
	}
	/**
	 * 
	* @author LK
	* @methods chiose 
	* @description 描述信息 选择配送方式
	* @date 2015-1-24 下午12:05:11 参数说明
	 */
	private void choose(){
		if(null == entity){
			showShortToast("请选择配送方式");
			return;
		}
		Intent intent = new Intent();
		intent.putExtra("methodVo", entity);
		setResult(SubmitOrderFormActivity.SHIPMETHOD, intent);
		this.finish();
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		methodAdapter.setSelect(position);
		SubmitOrderFormActivity.oldposition = position;
		entity = (ShipMethodVo)methodAdapter.getItem(position);
		if(Constants.ISSELF_CODE.equals(entity.getCode())){
			llZiquAddress.setVisibility(View.VISIBLE);
		}else{
			llZiquAddress.setVisibility(View.INVISIBLE);
		}
	}
	/**
	 * 
	* @author LK
	* @methods getZiquAddress 
	* @description 描述信息  获取自取地址
	* @date 2015-1-21 下午3:40:09 参数说明
	 */
	private void getZiquAddress(){
		final ProgressDialog progressDialog = new ProgressDialog(ShippingMethodActivity.this);
		try {
			progressDialog.show();
		} catch (Exception e) {
		}
		HttpUtils httpUtils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por","500");//请求接口
		params.addQueryStringParameter("p_id",pId); //产品ID
		params.addQueryStringParameter("shop_id",shopId);//商家ID
		params.addQueryStringParameter("goods_id",goodsId);//商品ID
		params.addQueryStringParameter("proIden",Constants.STOREFLAG);
		httpUtils.send(HttpMethod.POST, Constants.STORE_URL, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
				String result = responseInfo.result;
				if(!TextUtils.isEmpty(result)){
					AddressListVo entity = JSON.parseObject(result, AddressListVo.class);
					if(Constants.STORE_SUCCESS_CODE.equals(entity.getCode())){
						if(null != entity.getList() && entity.getList().size() > 0){
							View convertView = LayoutInflater.from(ShippingMethodActivity.this).inflate(R.layout.shop_item_ziqu_address, null);
							TextView tvAddress = ViewHolder.getViewById(convertView, R.id.tv_address);
							TextView tvTime = ViewHolder.getViewById(convertView, R.id.tv_time);
							TextView tvContact = ViewHolder.getViewById(convertView, R.id.tv_contact);
							TextView tvContactPhone = ViewHolder.getViewById(convertView, R.id.tv_contact_phone);
							TextView tvNote = ViewHolder.getViewById(convertView, R.id.tv_note);
							LinearLayout llCallPhone = ViewHolder.getViewById(convertView, R.id.ll_callphone);
							llCallPhone.setVisibility(View.GONE);
							AddressVo address = entity.getList().get(0);
							tvAddress.setText(String.format("地址：%s", address.getAddress()));
							tvTime.setText(String.format("时间：%s", address.getDraw_time()));
							tvContact.setText(String.format("联系人：%s", address.getConnect_name()));
							tvContactPhone.setText(String.format("电话：%s", address.getConnect_tel()));
							tvNote.setText(String.format("备注：%s", address.getNote()));
							llZiquAddress.addView(convertView);
						}
					}
				}
			}
		});
	}
}
